package com.binvshe.binvshe.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.OpusAdapter;
import com.binvshe.binvshe.organization.entity.OpusListEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * FileName: CollectionFragment
 *
 * @CretedBy : zofnk
 * @CreateDate: 2016/9/24 11:04
 * @E-mail: zofnk@vip.qq.com
 * @RevisionsDate：
 * @ModifyNotes：
 */
public class CollectionFragment extends SwipeListFragment {

    private int mPage = 0;

    public static CollectionFragment newInstance() {
        CollectionFragment fragment = new CollectionFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OpusAdapter(getContext());
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        startLoading();
    }

    @Override
    public void onStartLoading() {
        getFavoriteList();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        return super.onItemLongClick(arg0, arg1, position, itemId);
    }

    ListDataRequestListener<OpusListEntity> mRequestListener = new ListDataRequestListener(this, OpusListEntity.class);

    public void getFavoriteList() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(getContext());
            UrlParams params = new UrlParams();
            params.put(ParamKey.PAGE, mPage + "");
            params.put(ParamKey.SIZE, 8 + "");
            request.get(UrlCenter.FAVORITE, params, mRequestListener);
        }
    }
}
